package recharge_channel_management

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 充值渠道--列表
func NewRechargeChannelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelListLogic {
	return &RechargeChannelListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RechargeChannelListLogic) RechargeChannelList(req *types.RechargeChannelListRequest) (resp *types.RechargeChannelListResponse, err error) {
	rpcReq := rpc.RechargeChannelListRequest{
		Id:       req.Id,
		Name:     req.Name,
		Type:     req.Type,
		Status:   req.Status,
		Page:     req.Page,
		PageSize: req.PageSize,
	}

	rpcResp, err := l.svcCtx.AssetsRpc.PayRecharge.RechargeChannelList(l.ctx, &rpcReq)
	if err != nil {
		return nil, err
	}

	records := make([]types.RechargeChannel, len(rpcResp.Records))
	for i, record := range rpcResp.Records {
		records[i] = types.RechargeChannel{
			Id:           record.Id,
			CreatedAt:    record.CreatedAt,
			UpdatedAt:    record.UpdatedAt,
			DeletedAt:    record.DeletedAt,
			MappingCode:  record.MappingCode,
			Type:         record.Type,
			Name:         record.Name,
			Level:        record.Level,
			User_group:   record.UserGroup,
			BonusRate:    record.BonusRate,
			MinAmount:    record.MinAmount,
			MaxAmount:    record.MaxAmount,
			AmountConfig: record.AmountConfig,
			MerchantId:   record.MerchantId,
			AppId:        record.AppId,
			SecretKey:    record.SecretKey,
			Url:          record.Url,
			ReturnUrl:    record.ReturnUrl,
			NotifyUrl:    record.NotifyUrl,
			PubKey:       record.PubKey,
			PrivateKey:   record.PrivateKey,
			StartTime:    record.StartTime,
			EndTime:      record.EndTime,
			Sort:         record.Sort,
			Status:       record.Status,
			IsTestable:   record.IsTestable,
			WhiteList:    record.WhiteList,
			Remark:       record.Remark,
			Icon:         record.Icon,
			ChannelName:  record.ChannelName,
			INR:          record.Inr,
		}
	}

	return &types.RechargeChannelListResponse{
		Records: records,
		Pagination: types.Pagination{
			PageSize:  int32(rpcResp.Pagination.PageSize),
			Page:      int32(rpcResp.Pagination.Page),
			Total:     rpcResp.Pagination.Total,
			TotalPage: rpcResp.Pagination.TotalPage,
		},
		CommonResp: types.CommonResp{
			Code:    rpcResp.CommonResp.Code,
			Message: rpcResp.CommonResp.Message,
		},
	}, nil
}
